﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using AutoMapper;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using Infrastucture;
using WcfService1.DTO;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" Currency to change the class name "CurrencyServices" in code, svc and config file together.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [NHibernateContext]
    public class CurrencyServices : ICurrencyServices
    {
        private readonly CurrencyRepository CurrencyRepository;

        public CurrencyServices()
        {
            CurrencyRepository = new CurrencyRepository(new WcfSessionStorage());
        }

        #region ICurrencyServices Members

        public List<CurrencyDTO> GetList(int page, out long recordCount, string sortProperty = null,
                                         string sortType = "ASC")
        {
            var Currencys = new List<Currency>();
            var Results = new List<CurrencyDTO>();

            if (string.IsNullOrEmpty(sortProperty))
            {
                Currencys = CurrencyRepository.GetDataPaged(page, 30).ToList();
            }
            else
            {
                Currencys = CurrencyRepository.GetDataPaged(page, 30, sortProperty, sortType).ToList();
            }

            recordCount = CurrencyRepository.RecordCount;

            Currencys.ForEach(x =>
                                  {
                                      var CurrencyDto = new CurrencyDTO();
                                      Mapper.Map(x, CurrencyDto);
                                      //AutoMapper.Mapper.Map(x.ParentCurrency, CurrencyDto.ParentCurrency);
                                      Results.Add(CurrencyDto);
                                  }
                );

            return Results;
        }

        public bool Save(CurrencyDTO CurrencyDTO)
        {
            bool res;
            try
            {
                Currency Currency = CurrencyRepository.GetDataOne(x => x.CurrencyCode == CurrencyDTO.CurrencyCode);
                var user = new User();
                if (Currency == null)
                {
                    Currency = new Currency();
                    CurrencyDTO.created_date = DateTime.Now;
                    //AutoMapper.Mapper.Map(CurrencyDTO.created_by, user);
                    //Currency.modified_by = null;
                }
                else
                {
                    CurrencyDTO.modified_date = DateTime.Now;
                    //AutoMapper.Mapper.Map(CurrencyDTO.modified_by, user);
                }
                Mapper.Map(CurrencyDTO, Currency);
                CurrencyRepository.SaveOrUpdate(Currency);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }


        public bool Delete(CurrencyDTO CurrencyDto)
        {
            bool res;
            try
            {
                var Currency = new Currency();
                Mapper.Map(CurrencyDto, Currency);
                CurrencyRepository.Delete(Currency);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }


        public List<CurrencyDTO> GetListByCriteria(Dictionary<String, Object> criteria, string expression, int page,
                                                   out long recordCount)
        {
            var Currencys = new List<Currency>();
            var Results = new List<CurrencyDTO>();
            Currencys = CurrencyRepository.GetDataPaged(criteria, expression, page, 30).ToList();
            recordCount = CurrencyRepository.RecordCount;
            Currencys.ForEach(x =>
                                  {
                                      var CurrencyDto = new CurrencyDTO();
                                      Mapper.Map(x, CurrencyDto);
                                      Results.Add(CurrencyDto);
                                  }
                );

            return Results;
        }


        public List<CurrencyDTO> GetAllCurrency()
        {
            List<Currency> Currencys = CurrencyRepository.FetchAll().ToList();
            var Results = new List<CurrencyDTO>();
            Currencys.ForEach(x =>
                                  {
                                      var CurrencyDto = new CurrencyDTO();
                                      Mapper.Map(x, CurrencyDto);
                                      Results.Add(CurrencyDto);
                                  }
                );

            return Results;
        }


        public CurrencyDTO GetCurrency(String CurrencyCode)
        {
            Currency Currency = CurrencyRepository.GetDataOne(x => x.CurrencyCode == CurrencyCode);
            var CurrencyDto = new CurrencyDTO();
            Mapper.Map(Currency, CurrencyDto);
            return CurrencyDto;
        }

        public List<CurrencyDTO> GetCurrencyList(string CurrencyCode)
        {
            List<Currency> Currencys =
                CurrencyRepository.GetDataList(x => x.CurrencyCode.Contains(CurrencyCode)).ToList();
            var Results = new List<CurrencyDTO>();
            Currencys.ForEach(x =>
                                  {
                                      var CurrencyDto = new CurrencyDTO();
                                      Mapper.Map(x, CurrencyDto);
                                      Results.Add(CurrencyDto);
                                  }
                );

            return Results;
        }

        #endregion
    }
}